package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.SubOrder;
import cn.tedu.fresheveryday.pojo.vo.ProductSubOrderDetailVO;
import cn.tedu.fresheveryday.pojo.vo.SubOrderVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SubOrderMapper {
    /**
     * 添加子订单
     * @param subOrder
     */
    @Insert(
            "insert into sub_order (product_id, quantity, money, order_id) " +
            "values (#{productId}, #{quantity}, #{money}, #{orderId})"
    )
    void insert(SubOrder subOrder);

    /**
     * 批量添加子订单
     * @param list
     */
    void insertList(List<SubOrder> list);

    /**
     * 通过父订单号查询子订单
     * @param orderId 父订单号
     * @return
     */
    @Select(
            "select id, product_id, quantity, money, order_id from sub_order " +
            "where order_id = #{orderId}"
    )
    @Result(column = "order_id", property = "orderId")
    @Result(column = "product_id", property = "productId")
    List<SubOrder> selectByOrderId(String orderId);

    /**
     * 通过父订单号查询子订单
     * called by: OrderServiceImpl.selectByUserId(String userId)
     * called by: OrderServiceImpl.selectByStateAndUserId(int state, String userId)
     * @param orderId 父订单号
     * @return
     */
    @Select("select product_id, quantity, money from sub_order where order_id = #{orderId}")
    @Result(
            column = "product_id", property = "product", javaType = ProductSubOrderDetailVO.class,
            one = @One(
                    select = "cn.tedu.fresheveryday.mapper.ProductMapper.selectSubOrderDetailById"
            )
    )
    List<SubOrderVO> selectDetailsByOrderId(String orderId);

    /**
     * 通过父订单号删除子订单
     * @param orderId
     */
    @Delete("delete from sub_order where order_id = #{orderId}")
    void deleteByOrderId(String orderId);
}